// ==UserScript==
// @name         [kesai]百度网盘直接用pandownload打开
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  百度网盘直接用pandownload打开,这个需要我的桌面程序配合使用，哈哈，下载了也无法使用呢
// @author       kesai
// @match        https://pan.baidu.com/*
// @match        https://yun.baidu.com/*
// @require      https://cdn.bootcss.com/clipboard.js/1.7.1/clipboard.js
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    // Your code here...
    var winAppLink = "winapp://?D:\\百度下载\\PanDownload\\PanDownload.exe";
    
    function addScript(url) {
        var link = window.document.createElement('script');
        link.type = 'text/javascript';
        link.src = url + '?t=' + new Date().getTime();
        link.async = true;
        document.getElementsByTagName("HEAD")[0].appendChild(link);
    }

    if (typeof jQuery == 'undefined') {
        addScript("https://code.jquery.com/jquery-3.3.1.min.js");
    }

    window.onload = function() {
        var $btn = $('<a id="btn_pandownload" class="g-button g-button-blue"  href="javascript:;" title="PanDownload"><span class="g-button-right"><span class="text" style="width: auto;">PanDownload</span></span></a>');

        var clipboard = new Clipboard('#btn_pandownload');
        clipboard.on('success', function(e) {
            console.log('copy success!');            
            openWinApp(winAppLink);
        });
        clipboard.on('error', function(e) {
            console.error('Action:', e.action);
            console.error('Trigger:', e.trigger);
        });

        $btn.attr('data-clipboard-text', window.location.href);
        //百度网盘自带老版本jQuery($.fn.jquery=1.12.4),导致顺序有讲究,append前设置的属性和事件才有效,实际原因是啥无法确定
        $(".x-button-box").append($btn);
    }

    //独家自制的网页打开window程序
    function openWinApp(winAppLink) {
        window.open(winAppLink, "_self");
    }

})();